package com.bibrApp.difu.actvity.wallet_mvp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.my_mvp.MyActivity;
import com.bibrApp.difu.actvity.others.HelpActivity;
import com.bibrApp.difu.actvity.recharge_b_mvp.RechargeBActivity;
import com.bibrApp.difu.actvity.tibi_mvp.TibiActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.fragment.cloudwallet_mvp.CloudWalletBean;
import com.bibrApp.difu.fragment.cloudwallet_mvp.CloudWalletFragment;
import com.bibrApp.difu.fragment.heyuewallet_mvp.HeyueWalletFragment;
import com.bibrApp.difu.fragment.home_mvp.HomeFragment;
import com.bibrApp.difu.fragment.qianwallet_mvp.QianWalletAdapter;
import com.bibrApp.difu.fragment.qianwallet_mvp.QianWalletFragment;
import com.bibrApp.difu.fragment.wallet_mvp.CloudWalletAdapter;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.UIUtils;
import com.bibrApp.difu.widget.HomeViewPager;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

public class WalletActivity extends BaseActivity<WalletPresenter> implements IWalletView {


    //    @BindView(R.id.viewpager)
//    HomeViewPager viewPager;
    @BindView(R.id.tv_cloud)
    TextView tvCloud;
    @BindView(R.id.tv_duoqian)
    TextView tvDuoqian;
    @BindView(R.id.tv_heyue)
    TextView tvHeyue;
    @BindView(R.id.tv_nick)
    TextView tvNick;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.tv_select)
    TextView tvSelect;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;


    private static final String[] tabs = {"云钱包", "多签钱包", "合约钱包"};
    Map<String, Fragment> fragments = new HashMap<>();
    List<CloudWalletBean> datas = new ArrayList<>();
    private CloudWalletAdapter adapter;
    private PopupWindow popupWindow;

    @Override
    protected void initPresenter() {
        presenter = new WalletPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wallet;
    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {

    }

    @Override
    public void onDataResult(List<CloudWalletBean> list) {
        datas.addAll(list);
        adapter.setNewData(datas);
    }

    @Override
    public void initView() {
        tvMoney.setText(getIntent().getStringExtra("money"));
        tvNick.setText(TextUtils.isEmpty(app.getNickName()) ? app.getuId() : app.getNickName());

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CloudWalletAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            if (view.getId() == R.id.tv_tibi) {
                Intent intent = new Intent(WalletActivity.this, TibiActivity.class);
                intent.putExtra("bean", adapter.getItem(position));
                intent.putExtra("wallet", getWalletName(selectIndex));
                intent.putExtra("type",selectIndex);
                startActivity(intent);
            }else if(view.getId() == R.id.tv_chongzhi){
                Bundle bundle = new Bundle();
                bundle.putString("name", Objects.requireNonNull(adapter.getItem(position)).getCoinEunit());
                IntentUtil.toActivity(RechargeBActivity.class ,this,bundle);
            }
        });

        presenter.getDatas();
//        viewPager.setNoScroll(true);
//        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
//            @NonNull
//            @Override
//            public Fragment getItem(int position) {
//                Fragment fragment = fragments.get(tabs[position]);
//                if (fragment != null) {
//                    return fragment;
//                }
//                if (position == 0) {
//                    fragment = new CloudWalletFragment();
//                } else if (position == 1) {
//                    fragment = new QianWalletFragment();
//                } else {
//                    fragment = new HeyueWalletFragment();
//                }
//                fragments.put(tabs[position], fragment);
//                return fragment;
//            }
//
//            @Override
//            public int getCount() {
//                return tabs.length;
//            }
//        });
    }

    private String getWalletName(int selectIndex) {
        if (selectIndex == 0) {
            return "云钱包";
        } else {
            return selectIndex == 1 ? "多签钱包" : "合约钱包";
        }
    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        onBackPressed();
    }

    @OnClick(R.id.bt_help)
    public void helpClick() {
        startActivity(new Intent(this, HelpActivity.class));
    }


    int selectIndex = 0;

    @OnClick(R.id.tv_cloud)
    public void cloudClick() {
        if (selectIndex == 0) {
            return;
        }
        tvCloud.setTextColor(getResources().getColor(R.color.blue_e4));
        tvDuoqian.setTextColor(getResources().getColor(R.color.black_3));
        tvHeyue.setTextColor(getResources().getColor(R.color.black_3));
        tvInfo.setVisibility(View.GONE);
        tvSelect.setVisibility(View.GONE);
        adapter.setNewData(datas);
        adapter.setType(0);
        selectIndex = 0;


        //viewPager.setCurrentItem(0);
    }

    @OnClick(R.id.tv_duoqian)
    public void duoqianClick() {
        if (selectIndex == 1) {
            return;
        }
        tvCloud.setTextColor(getResources().getColor(R.color.black_3));
        tvDuoqian.setTextColor(getResources().getColor(R.color.blue_e4));
        tvHeyue.setTextColor(getResources().getColor(R.color.black_3));
        tvInfo.setVisibility(View.VISIBLE);
        tvSelect.setVisibility(View.VISIBLE);
        tvInfo.setText("选择多签钱包");
        tvSelect.setHint("选择多签号");
        tvSelect.setText("");
        adapter.setNewData(new ArrayList<>());
        adapter.setType(1);
        selectIndex = 1;
        duoqianSelectClick();

        //viewPager.setCurrentItem(1);
    }

    @OnClick(R.id.tv_heyue)
    public void heyueClick() {
        if (selectIndex == 2) {
            return;
        }
        tvCloud.setTextColor(getResources().getColor(R.color.black_3));
        tvDuoqian.setTextColor(getResources().getColor(R.color.black_3));
        tvHeyue.setTextColor(getResources().getColor(R.color.blue_e4));
        tvInfo.setVisibility(View.VISIBLE);
        tvSelect.setVisibility(View.VISIBLE);
        tvInfo.setText("选择合约钱包");
        tvSelect.setHint("选择合约号");
        tvSelect.setText("");
        adapter.setNewData(new ArrayList<>());
        adapter.setType(2);
        selectIndex = 2;

        heyueSelectClick();


        //viewPager.setCurrentItem(2);
    }

    @OnClick(R.id.tv_select)
    public void SelectClick() {
        if (selectIndex == 1) {
            duoqianSelectClick();
        } else {
            heyueSelectClick();
        }
    }

    @OnClick(R.id.iv_avater)
    public void clickAvater(){
        startActivity(MyActivity.class);
    }

    private void heyueSelectClick() {
        List<CloudWalletBean> list = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            if (!datas.get(i).getCoinEunit().equals("BTC")) {
                list.add(datas.get(i));
            }
        }
        if (list.size() < 2) {
            return;
        }

        View layout = View.inflate(this, R.layout.pop_wallet_type, null);

        TextView tvText1 = layout.findViewById(R.id.tv_all);
        TextView tvText2 = layout.findViewById(R.id.tv_btc);
        TextView tvText3 = layout.findViewById(R.id.tv_eth);
        tvText1.setText("选择合约号");
        tvText2.setText(list.get(0).getCoinId());
        tvText3.setText(list.get(1).getCoinId());
        tvText2.setOnClickListener(view -> {
            tvSelect.setText(list.get(0).getCoinId());
            adapter.setNewData(Arrays.asList(list.get(0)));
            popupWindow.dismiss();
        });
        tvText3.setOnClickListener(view -> {
            tvSelect.setText(list.get(1).getCoinId());
            adapter.setNewData(Arrays.asList(list.get(1)));
            popupWindow.dismiss();
        });

        popupWindow = new PopupWindow(layout, UIUtils.getWidth(this) - UIUtils.dpToPixels(this, 24), -2, true);
        popupWindow.setFocusable(true);

        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(tvSelect, 0, 0);

    }

    private void duoqianSelectClick() {
        int index = -1;
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getCoinEunit().equals("BTC")) {
                index = i;
            }
        }

        if (index == -1) {
            return;
        }

        CloudWalletBean cloudWalletBean = datas.get(index);
        View layout = View.inflate(this, R.layout.pop_wallet_type, null);

        TextView tvText1 = layout.findViewById(R.id.tv_all);
        TextView tvText2 = layout.findViewById(R.id.tv_btc);
        TextView tvText3 = layout.findViewById(R.id.tv_eth);
        tvText3.setVisibility(View.GONE);
        tvText1.setText("选择多签号");
        tvText2.setText(cloudWalletBean.getCoinId());
        tvText2.setOnClickListener(view -> {
            tvSelect.setText(cloudWalletBean.getCoinId());
            adapter.setNewData(Arrays.asList(cloudWalletBean));
            popupWindow.dismiss();
        });

        popupWindow = new PopupWindow(layout, UIUtils.getWidth(this) - UIUtils.dpToPixels(this, 24), -2, true);
        popupWindow.setFocusable(true);

        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(tvSelect, 0, 0);
    }
}
